<template>
  <div>
    <el-card class="box-card" style="width: 400px">
      <div slot="header" class="clearfix">
        <span>{{ user.name }}</span>
      </div>
      <div>
        <div style="display: flex;justify-content: center">
          <img title="点击修改头像" :src="user.headImage" alt="" style="height: 100px;width: 100px;border-radius: 50px">
        </div>
        <div>
          用户账号:<el-tag>{{user.username}}</el-tag>
        </div>
        <div>
          手机号码:<el-tag>{{user.telephone}}</el-tag>
        </div>
        <div>
          居住地址:<el-tag>{{user.address}}</el-tag>
        </div>
        <div>
          用户标签<el-tag type="success" v-for="(r,index) in user.roles" :key="index">{{r.description}}</el-tag>
        </div>

        <div style="display: flex;justify-content: space-around;margin-top: 10px">
          <el-button type="success" @click="showEditView">修改信息</el-button>
          <el-button type="danger" @click="showPasswordView">修改密码</el-button>
        </div>

      </div>
    </el-card>

    <el-dialog title="更新操作员资料" :visible.sync="dialogFormVisible">
      <div>
        <table>
          <tr>
            <td>用户昵称:</td>
            <td><el-input v-model="user2.name"></el-input></td>
          </tr>
          <tr>
            <td>手机号码:</td>
            <td><el-input v-model="user2.telephone"></el-input></td>
          </tr>
          <tr>
            <td>居住地址:</td>
            <td><el-input v-model="user2.address"></el-input></td>
          </tr>
          <tr>
            <td>用户性别:</td>
            <td><el-select v-model="user2.sex" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select></td>
          </tr>
        </table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdateUser">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="更新密码" :visible.sync="dialogFormVisible2">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="请输入旧密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请输入新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getRequest} from "../utils/api";

export default {
  name: "UserInfo",
  data(){

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      user:'',
      dialogFormVisible:false,
      user2:'',
      dialogFormVisible2:false,
      ruleForm: {
        pass: '',
        checkPass: '',
        oldPass:'',
        userId:''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        oldPass: [
          { validator: validatePass, trigger: 'blur' }
        ],
      }
    };


  },
  mounted() {
    this.initUser();
  },
  methods:{
    //获取登录用户信息
    initUser(){
      this.getRequest('/getUser').then(resp=>{
        if(resp){
          this.user=resp;
          this.user2=Object.assign({},this.user);
          window.sessionStorage.setItem('user',JSON.stringify(resp));
          this.$store.commit('INIT_USER',resp);
        }
      })
    },
    //显示编辑框
    showEditView(){
      this.dialogFormVisible=true;
    },
    //更新用户信息
    doUpdateUser(){
      this.putRequest('/updateUser',this.user2).then(resp=>{
        if(resp){
          this.initUser();
          this.dialogFormVisible=false;

        }
      })
    },
    //显示密码修改框
    showPasswordView(){
      this.dialogFormVisible2=true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.userId=this.user.id;
          this.putRequest('/updateUserPassword',this.ruleForm).then(resp=>{
            if(resp){
              //更新成功后退出登录
              this.getRequest("/logout")
              window.sessionStorage.removeItem("user");
              window.sessionStorage.removeItem("tokenStr");
              this.$store.commit("initRoutes",[]);
              this.$router.replace("/");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>